package sys_serv

import (
	"strconv"
	"strings"
	"ubt-rbac-service/internal/app/model"
	"ubt-rbac-service/internal/app/model/sys_mod"
	"ubt-rbac-service/internal/app/util"
)

type sysTag struct{}

var SysTagServ = sysTag{}

// Add 新增标签
func (s *sysTag) Add(req *sys_mod.SysTagSaveReq) *model.Result {
	sqlStr := "INSERT INTO sys_tag (tag_id, tag_name, kind, states, add_time, opt_org) VALUES ($1, $2, $3, 1, EXTRACT(EPOCH FROM NOW()), $4)"
	retCode, retMsg := util.CudData(sqlStr, req.TagId, req.TagName, req.Kind, req.OptOrg)
	return &model.Result{RetCode: retCode, RetMsg: retMsg}
}

// Exists 判断标签是否存在
func (s *sysTag) Exists(req *sys_mod.SysTagExtVo) bool {
	sqlStr := "SELECT COUNT(1) AS nums FROM sys_tag WHERE states=1 AND kind=$1 AND tag_name=$2"
	param := make([]interface{}, 0)
	param = append(param, req.Kind)
	param = append(param, req.TagName)
	if req.TagId != "" {
		sqlStr += " AND tag_id!=$3"
		param = append(param, req.TagId)
	}
	if dbMap, err := util.GetOne(sqlStr, param...); err == nil && len(dbMap) > 0 {
		if dbMap["nums"].(int64) > 0 {
			return true
		}
	}
	return false
}

// Update 修改标签
func (s *sysTag) Update(req *sys_mod.SysTagSaveReq) *model.Result {
	sqlStr := "UPDATE sys_tag SET tag_name=$1, upd_time=EXTRACT(EPOCH FROM NOW()) WHERE tag_id=$2"
	retCode, retMsg := util.CudData(sqlStr, req.TagName, req.TagId)
	return &model.Result{RetCode: retCode, RetMsg: retMsg}
}

// Delete 删除标签
func (s *sysTag) Delete(tagId string) *model.Result {
	sqlStr := "UPDATE sys_tag SET states=2, upd_time=EXTRACT(EPOCH FROM NOW()) WHERE tag_id=$1"
	retCode, retMsg := util.CudData(sqlStr, tagId)
	return &model.Result{RetCode: retCode, RetMsg: retMsg}
}

// Deletes 处理标签
func (s *sysTag) Deletes(tagIds string) *model.Result {
	var (
		param []interface{}
		q     string
		n     = 0
	)
	idList := strings.Split(tagIds, ",")
	for _, curId := range idList {
		n++
		q += "$" + strconv.Itoa(n) + ","
		param = append(param, curId)
	}
	q = q[:len(q)-1]
	sqlStr := "UPDATE sys_tag SET states=2, upd_time=EXTRACT(EPOCH FROM NOW()) WHERE tag_id IN (" + q + ")"
	retCode, retMsg := util.CudData(sqlStr, param...)
	return &model.Result{RetCode: retCode, RetMsg: retMsg}
}

// Count 获取标签数量
func (s *sysTag) Count(req *sys_mod.SysTagPageReq) *model.Result {
	sqlStr := "SELECT COUNT(1) AS nums FROM sys_tag WHERE states=1 AND kind=$1 AND opt_org=$2"
	param := make([]interface{}, 0)
	param = append(param, req.Kind)
	param = append(param, req.OptOrg)
	var n = 2
	if req.SchName != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.SchName, " ")
		for _, name := range nameArr {
			n++
			sqlStr += "tag_name LIKE '%'||$" + strconv.Itoa(n) + "||'%' OR "
			param = append(param, name)
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	if dbMap, err := util.GetOne(sqlStr, param...); err == nil && len(dbMap) > 0 {
		return &model.Result{RetCode: 1001, RetMsg: "ok", RetData: dbMap["nums"]}
	} else {
		return &model.Result{RetCode: 1000, RetMsg: "暂无内容"}
	}
}

// Page 获取标签分页列表
func (s *sysTag) Page(req *sys_mod.SysTagPageReq) *model.Result {
	sqlStr := "SELECT tag_id, tag_name, add_time FROM sys_tag WHERE states=1 AND kind=$1 AND opt_org=$2"
	param := make([]interface{}, 0)
	param = append(param, req.Kind)
	param = append(param, req.OptOrg)
	var n = 2
	if req.SchName != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.SchName, " ")
		for _, name := range nameArr {
			n++
			sqlStr += "tag_name LIKE '%'||$" + strconv.Itoa(n) + "||'%' OR "
			param = append(param, name)
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	sqlStr += " ORDER BY add_time DESC LIMIT $" + strconv.Itoa(n+1) + " OFFSET $" + strconv.Itoa(n+2)
	param = append(param, req.Limit)
	param = append(param, req.Offset)
	dbMap, err := util.GetAll(sqlStr, param...)
	if err == nil && len(dbMap) > 0 {
		return &model.Result{RetCode: 1001, RetMsg: "ok", RetData: dbMap}
	} else {
		return &model.Result{RetCode: 1000, RetMsg: "暂无内容"}
	}
}

// Select 查询标签是否已存在，存在则返回
func (s *sysTag) Select(req *sys_mod.SysTagSelReq) *sys_mod.SysTagSelRes {
	sqlStr := "SELECT tag_id, tag_name FROM sys_tag WHERE states=1 AND kind=$1 AND opt_org=$2 AND tag_name=$3 LIMIT 1"
	dbMap, err := util.GetOne(sqlStr, req.Kind, req.OptOrg, req.TagName)
	if err == nil && len(dbMap) > 0 {
		return &sys_mod.SysTagSelRes{TagId: dbMap["tagId"].(string), TagName: dbMap["tagName"].(string)}
	} else {
		return nil
	}
}
